package com.smarttest.portal.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smarttest.portal.mapper.DbAutoSettingMapper;
import com.smarttest.portal.pojo.DbAutoSetting;
import com.smarttest.portal.pojo.DbAutoSettingExample;
import com.smarttest.portal.pojo.DbAutoSettingExample.Criteria;
import com.smarttest.portal.service.AutoSetting;
@Service
public class AutoSettingImpl implements AutoSetting {
	@Autowired
	DbAutoSettingMapper DbAutoSettingMapper;
	@Override
	public int modifySetting (int envid,Integer moduleid, int type, String setting) {
		DbAutoSettingExample example = new DbAutoSettingExample();
		Criteria c = example.createCriteria();
		c.andModuleidEqualTo(moduleid);
		c.andTypeEqualTo(type);
		List<DbAutoSetting> ls = DbAutoSettingMapper.selectByExample(example);
		if (ls.size() > 0) {
			DbAutoSetting db = ls.get(0);
			db.setSetting(setting);
			return DbAutoSettingMapper.updateByExampleSelective(db, example);
		}
		else {
			DbAutoSetting ds = new DbAutoSetting();
			ds.setEnvid(envid);
			ds.setModuleid(moduleid);
			ds.setType(type);
			return DbAutoSettingMapper.insert(ds);
		}		
	}
	
	@Override
	public List<DbAutoSetting> getSetting (int envid,Integer moduleid, int type){
		if (null != moduleid) {
			DbAutoSettingExample example = new DbAutoSettingExample();
			Criteria c = example.createCriteria();
			c.andModuleidEqualTo(moduleid);
			c.andTypeEqualTo(type);
			c.andEnvidEqualTo(envid);
			return DbAutoSettingMapper.selectByExampleWithBLOBs(example);
		}
		else {
			return new ArrayList<DbAutoSetting>();
		}
	}
}
